import { connect } from 'mongoose';
import { v4 as uuidv4 } from 'uuid';
import CardList from '../models/cardList';
import Card from '../models/card';
import { CheckList } from '../models/checkList';
import { Check } from '../models/check';
import { mongodb } from '../config/db';

// 测试数据
const cardListsData = [
    { 
        title: '待办事项', 
        cards: [
            { 
                title: '完成用户注册功能', 
                content: '实现用户注册流程和表单验证',
                checkLists: [
                    { 
                        title: '前端注册页面', 
                        checks: ['设计注册表单', '添加表单验证', '实现提交逻辑', '优化用户体验'] 
                    },
                    { 
                        title: '后端注册接口', 
                        checks: ['设计用户模型', '实现注册逻辑', '添加数据验证', '集成短信验证'] 
                    }
                ]
            },
            { 
                title: '实现数据验证', 
                content: '为系统关键接口添加数据验证机制',
                checkLists: [
                    { 
                        title: '输入验证', 
                        checks: ['添加 Joi 验证', '编写验证规则', '处理验证错误', '添加国际化错误提示'] 
                    }
                ]
            },
            {
                title: '性能优化',
                content: '分析并优化系统性能瓶颈',
                checkLists: [
                    {
                        title: '数据库优化',
                        checks: ['添加索引', '优化查询', '配置缓存', '分析慢查询']
                    },
                    {
                        title: '前端性能',
                        checks: ['代码分割', '懒加载', '图片压缩', '减少重复渲染']
                    }
                ]
            }
        ]
    },
    { 
        title: '进行中', 
        cards: [
            { 
                title: '首页布局设计', 
                content: '设计并实现系统首页的整体布局',
                checkLists: [
                    { 
                        title: 'UI 组件', 
                        checks: ['设计卡片组件', '实现响应式布局', '添加交互效果', '适配移动端'] 
                    },
                    {
                        title: '性能监控',
                        checks: ['接入性能监控', '配置告警', '分析用户行为']
                    }
                ]
            },
            {
                title: '权限管理系统',
                content: '设计并实现细粒度的权限控制',
                checkLists: [
                    {
                        title: '角色管理',
                        checks: ['设计角色模型', '实现角色CRUD', '配置默认权限', '支持自定义权限']
                    },
                    {
                        title: '权限控制',
                        checks: ['前端路由拦截', '后端接口鉴权', '权限缓存', '动态权限更新']
                    }
                ]
            }
        ]
    },
    {
        title: '已完成',
        cards: [
            {
                title: '项目初始化',
                content: '搭建项目基础架构和开发环境',
                checkLists: [
                    {
                        title: '技术选型',
                        checks: ['选择前端框架', '选择后端框架', '确定数据库', '配置开发工具']
                    },
                    {
                        title: '环境配置',
                        checks: ['搭建开发环境', '配置持续集成', '部署流程', '性能监控']
                    }
                ]
            }
        ]
    }
];

// 生成测试数据的函数
async function generateTestData() {
    try {
        // 连接数据库
        await connect(`mongodb://${mongodb.host}:${mongodb.port || '27017'}/${mongodb.db}`);
        console.log('Connected to MongoDB');

        // 清空现有数据
        await CardList.deleteMany({});
        await Card.deleteMany({});
        await CheckList.deleteMany({});
        await Check.deleteMany({});
        console.log('Cleared existing data');

        // 创建卡片列表和卡片
        for (let i = 0; i < cardListsData.length; i++) {
            const listData = cardListsData[i];
            const listId = uuidv4();

            // 创建卡片列表
            const cardList = await CardList.create({
                listId,
                title: listData.title,
                order: i,
                createdAt: Date.now(),
                version: 0
            });

            // 为每个列表创建卡片
            for (let j = 0; j < listData.cards.length; j++) {
                const cardData = listData.cards[j];
                const cardId = uuidv4();

                const card = await Card.create({
                    cardId,
                    listId,
                    title: cardData.title,
                    content: cardData.content,
                    order: j,
                    createdAt: Date.now(),
                    version: 0
                });

                // 为每张卡片创建 CheckList 和 Checks
                for (let k = 0; k < cardData.checkLists.length; k++) {
                    const checkListData = cardData.checkLists[k];
                    const checkListId = uuidv4();
                    const checkList = await CheckList.create({
                        checkListId,
                        cardId,
                        title: checkListData.title,
                        createdAt: Date.now(),
                        updatedAt: Date.now()
                    });

                    // 为每个 CheckList 创建 Checks
                    for (let l = 0; l < checkListData.checks.length; l++) {
                        const checkId = uuidv4();
                        await Check.create({
                            checkId,
                            checkListId,
                            title: checkListData.checks[l],
                            completed: false,
                            createdAt: Date.now(),
                            updatedAt: Date.now()
                        });
                    }
                }
            }

            console.log(`Created card list: ${listData.title}`);
        }

        console.log('Test data generation completed');
        process.exit(0);
    } catch (error) {
        console.error('Error generating test data:', error);
        process.exit(1);
    }
}

// 运行生成测试数据的函数
generateTestData();